package com.shuyuntu.proposal.controller;

import com.shuyuntu.proposal.entity.Delegate;
import com.shuyuntu.proposal.security.UseSecurity;
import com.shuyuntu.proposal.service.DelegateService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/delegate")
public class DelegateController {
    /**
     * 获取教师代表的信息
     * @param userId 教师代表的id
     * @return 教师代表的VO，返回为JSON数组
     */
    @ResponseBody
    @RequestMapping(value = "/{userId}", method = RequestMethod.GET)
    @UseSecurity(category = "01")
    public Delegate getDelegateInfo(@PathVariable String userId, HttpSession session) {
        Delegate delegate = delegateService.getDelegateById(userId);

        if (delegate != null) {
            return delegate;
        } else {
            return null;
        }
    }

    /**
     * 获取所有用户列表,在角色管理的时候使用
     *
     * @param session injection
     */
    @UseSecurity(category = "0601")
    @ResponseBody
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public List<Delegate> getAllDelegateInfo(HttpSession session) {
        List<Delegate> delegateList = delegateService.getAllDelegateInfo();
        return delegateList;
    }

    /**
     * 跳转到教师代表的首页
     * 如果检查到访问其它用户的首页,则返回到登录界面
     *
     * @param userId 登录用户的id
     */
    @RequestMapping("/index/{userId}")
    public String index(@PathVariable String userId, HttpSession session,
                        HttpServletResponse response) {
        if (userId != null && userId.equals(session.getAttribute("loginUserId"))) {
            return "delegate/index";
        }
        response.setStatus(404);
        return "redirect:/login";
    }

    /******************单页面跳转需要用到的资源*******************/
    @RequestMapping("/createProposal")
    public String createProposal() {
        return "delegate/createProposal";
    }
    @RequestMapping("/mainContent")
    public String mainContent() {
        return "delegate/mainContent";
    }
    @RequestMapping("/viewMessages")
    public String viewMessages() {
        return "delegate/viewMessages";
    }
    @RequestMapping("/myProposal")
    public String myProposal() {
        return "delegate/myProposal";
    }
    @RequestMapping("/myDraft")
    public String myDraft() {
        return "delegate/myDraft";
    }
    @RequestMapping("/checkProposals")
    public String checkProposals(HttpSession session) {
        return "delegate/checkProposals/checkProposals";
    }
    @RequestMapping("/checkProposal")
    public String checkProposal(HttpSession session) {
        return "delegate/checkProposals/checkProposal";
    }
    @RequestMapping("/viewProposal")
    public String viewProposal() {
        return "delegate/viewProposal";
    }
    @RequestMapping("/queryProposal/queryWaitMotionProposal")
    public String waitMotionProposal() {
        return "delegate/queryProposal/queryWaitMotionProposal";
    }
    @RequestMapping("/systemConfig")
    public String systemConfig() {
        return "delegate/systemConfig/systemConfig";
    }
    @RequestMapping("/roleManage")
    public String roleManage() {
        return "delegate/systemConfig/roleManage";
    }
    @RequestMapping("/handleRegist")
    public String handleRegist() {
        return "delegate/handleRegist/listProposal";
    }
    @RequestMapping("/registProposal")
    public String registProposal() {
        return "delegate/handleRegist/registProposal";
    }
    @RequestMapping("/assistProposals")
    public String assistProposals() {
        return "delegate/myDepartment/assistProposals";
    }
    @RequestMapping("/proposalFeedback")
    public String proposalFeedback() {
        return "delegate/myDepartment/proposalFeedback";
    }
    @RequestMapping("/puttedIntoEffectProposals")
    public String puttedIntoEffectProposals() {
        return "delegate/myDepartment/puttedIntoEffectProposals";
    }
    @RequestMapping("/waitFeedbackProposals")
    public String waitFeedbackProposals() {
        return "delegate/myDepartment/waitFeedbackProposals";
    }
    @RequestMapping("/queryProposal/proposalSearch")
    public String proposalSearch() {
        return "delegate/queryProposal/proposalSearch";
    }
    @RequestMapping("/otherArticle/listPage")
    public String noticeList() {
        return "delegate/otherArticle/listPage";
    }
    @RequestMapping("/otherArticle/createPage")
    public String otherCreatePage() {
        return "delegate/otherArticle/createPage";
    }
    @RequestMapping("/otherArticle/detailPage")
    public String otherDetailPage() {
        return "delegate/otherArticle/detailPage";
    }
    @RequestMapping("/exit")
    public String onExit(HttpSession session) {
        session.setAttribute("loginUserId", null);
        return "redirect:/login";
    }
    /************************依赖注入相关↓**************************/
    @Resource
    DelegateService delegateService;

    public DelegateService getDelegateService() {
        return delegateService;
    }

    public void setDelegateService(DelegateService delegateService) {
        this.delegateService = delegateService;
    }
}
